from sklearn.cluster import DBSCAN,OPTICS,KMeans
import numpy as np
import matplotlib.pyplot as plt

def test():
    # estimator=DBSCAN(eps=20,min_samples=15,metric='euclidean')
    # estimator=OPTICS(eps=2,min_samples=15,metric='euclidean')
    estimator=KMeans(n_clusters=100)
    fileName = "../../synthetic/100_100_data.txt"
    data = []
    with open(fileName,"r",encoding='utf-8') as f :
        dataTemp = f.readlines()
        for i in range(1,len(dataTemp)):
            dataTemp2 = dataTemp[i].split(' ')
            data.append([float(dataTemp2[1]),float(dataTemp2[2])])
    data = np.array(data)
    # print(data)
    estimator.fit(data)
    labels = estimator.labels_
    print(labels)
    clusterNum=len(set(labels))
    fig = plt.figure()
    scatterColors = ['black', 'blue', 'green', 'yellow', 'red', 'purple', 'orange', 'brown']
    ax = fig.add_subplot(111)
    for i in range(-1,clusterNum):
        colorSytle = scatterColors[i % len(scatterColors)]
        subCluster = data[np.where(labels==i)]
        ax.scatter(subCluster[:,0], subCluster[:,1], c=colorSytle, s=12)
    plt.savefig("dbscan.png")


if __name__ =="__main__":
    test()
